package com.holly.common_view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.holly.common.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * 2016/11/8 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public abstract class BaseListViewAdapter<Bean> extends android.widget.BaseAdapter {
    protected final String TAG = L.createTag(this);
    protected List<Bean> mList = new ArrayList<Bean>();

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecyclerView.ViewHolder holder;
        if (convertView == null) {
            holder = onCreateViewHolder(parent, getItemViewType(position));
            convertView = holder.itemView;
            convertView.setTag(holder);
        } else {
            holder = (RecyclerView.ViewHolder) convertView.getTag();
        }
        onBindViewHolder(holder,position);
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setList(List<Bean> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
            L.d(TAG, "setList.size = " + mList.size());
        } else {
            L.d(TAG, "setList: size = 0");
        }
    }

    public List<Bean> getList() {
        List list = new ArrayList();
        list.addAll(mList);
        L.d(TAG, "getList.size = " + list.size());
        return list;
    }

    protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
        if (holder instanceof BaseHolder) {
            BaseHolder baseHolder= (BaseHolder) holder;
            baseHolder.mPosition=position;
            baseHolder.bean=mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        return null;
    }
}
